package com.jy.modules.cims.checkhealthy.service;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.data.common.dao.TCMmCheckHealthyDao;
import com.jy.modules.cims.data.common.dto.TCMmCheckHealthyDTO;
import com.jy.modules.cims.data.common.dto.TCMmHealthyRsDTO;
import com.jy.platform.core.common.BaseDTO;

/**
 * @classname: TCMmCheckHealthyService
 * @description: 定义  T_C_MM_CHECK_HEALTHY 实现类
 * @author:  Administrator
 */
@Service("com.jy.modules.cims.checkhealthy.service.TCMmCheckHealthyService")
public class TCMmCheckHealthyService implements Serializable {

    private static final long serialVersionUID = 1L;
    
    private static final Logger logger = LoggerFactory.getLogger(TCMmCheckHealthyService.class);
    
	@Autowired
	private TCMmCheckHealthyDao dao;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.checkhealthy.service.TCMmHealthyRsService")
	private TCMmHealthyRsService rsservice;

	/**
     * @author Administrator
     * @description: 分页查询 T_C_MM_CHECK_HEALTHY列表
     * @date 2016-10-18 14:46:54
     * @param searchParams 条件
     * @return
     * @throws
     */ 
	public List<TCMmCheckHealthyDTO> searchTCMmCheckHealthyByPaging(Map<String,Object> searchParams) throws Exception {
		List<TCMmCheckHealthyDTO> dataList =  dao.searchTCMmCheckHealthyByPaging(searchParams);
		return dataList;
	}
	/**
     * @author Administrator
     * @description: 按条件查询T_C_MM_CHECK_HEALTHY列表
     * @date 2016-10-18 14:46:54
     * @param searchParams 条件
     * @return
     * @throws
     */
	public List<TCMmCheckHealthyDTO> searchTCMmCheckHealthy(Map<String,Object> searchParams) throws Exception {
	    List<TCMmCheckHealthyDTO> dataList = dao.searchTCMmCheckHealthy(searchParams);
        return dataList;
    }
	/**
     * @author Administrator
     * @description: 查询T_C_MM_CHECK_HEALTHY对象
     * @date 2016-10-18 14:46:54
     * @param id
     * @return
     * @throws
     */ 
	public TCMmCheckHealthyDTO queryTCMmCheckHealthyByPrimaryKey(String id) throws Exception {
		
		TCMmCheckHealthyDTO dto = dao.findTCMmCheckHealthyByPrimaryKey(id);
		
		if(dto == null) dto = new TCMmCheckHealthyDTO();
		
		return dto;
		
	}

	/**
     * @title: insertTCMmCheckHealthy
     * @author Administrator
     * @description: 新增 T_C_MM_CHECK_HEALTHY对象
     * @date 2016-10-18 14:46:54
     * @param dto
     * @return
     * @throws
     */
	@SuppressWarnings("all")
	public Long insertTCMmCheckHealthy(TCMmCheckHealthyDTO dto) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);
		
		int count = dao.insertTCMmCheckHealthy(paramMap);
		
		TCMmCheckHealthyDTO resultDto = (TCMmCheckHealthyDTO) paramMap.get("dto");
		Long keyId = resultDto.getId();
		return keyId;
	}
	/**
     * @title: updateTCMmCheckHealthy
     * @author Administrator
     * @description: 修改 T_C_MM_CHECK_HEALTHY对象
     * @date 2016-10-18 14:46:54
     * @param paramMap
     * @return
     * @throws
     */
	public void updateTCMmCheckHealthy(TCMmCheckHealthyDTO dto) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);
		
		dao.updateTCMmCheckHealthy(paramMap);
	}
	/**
     * @title: deleteTCMmCheckHealthyByPrimaryKey
     * @author Administrator
     * @description: 删除 T_C_MM_CHECK_HEALTHY,按主键
     * @date 2016-10-18 14:46:54
     * @param paramMap
     * @throws
     */ 
	public void deleteTCMmCheckHealthyByPrimaryKey(BaseDTO baseDto,String ids) throws Exception {
		if(StringUtils.isEmpty(ids)) throw new Exception("删除失败！传入的参数主键为null");
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", baseDto);
		paramMap.put("ids", ids);
		dao.deleteTCMmCheckHealthyByPrimaryKey(paramMap);
	}
	
	/**
	 * 执行已配置的健康检查sql
	 * @throws Exception 
	 * @Title doCheckHealthyEveryday
	 * @Description TODO 
	 * void 
	 * @author dell-win7
	 * @date 2016-10-19-上午10:10:09
	 * @update 
	 * @throws
	 */
	public void doCheckHealthyEveryday() throws Exception{
//		this.doDeleteCheckHealthyRs();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		TCMmCheckHealthyDTO dto = new TCMmCheckHealthyDTO();
		dto.setValidateState("1");
		paramMap.put("dto", dto);
		List<TCMmCheckHealthyDTO> checkSqlList = this.searchTCMmCheckHealthy(paramMap);
		Date today = new Date();
		
		for (TCMmCheckHealthyDTO tcMmCheckHealthyDTO : checkSqlList) {
			if(tcMmCheckHealthyDTO.getHSql()==null||"".equals(tcMmCheckHealthyDTO.getHSql().trim())){
				continue;
			}
			try {
				Integer rs = dao.searchCheckHealthyByHsql(tcMmCheckHealthyDTO.getHSql());
				if(rs!=null&&rs>0){
					TCMmHealthyRsDTO rsdto = new TCMmHealthyRsDTO();
					rsdto.setHId(tcMmCheckHealthyDTO.getId());
					rsdto.setHRs(tcMmCheckHealthyDTO.getHName()+":"+String.valueOf(rs));
					rsdto.setBizDate(today);
					rsdto.setCreateTime(today);
					rsdto.setModifyTime(today);
					rsservice.insertTCMmHealthyRs(rsdto);
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("sql 执行异常："+e.getMessage());
			}
			
		}
	}
	
	/**
	 * 清空结果表
	 * @Title doDeleteCheckHealthyRs
	 * @Description TODO 
	 * void 
	 * @author lizhiqiang
	 * @date 2016-10-20-下午6:06:59
	 * @update 
	 * @throws
	 */
	public void doDeleteCheckHealthyRs(){
		dao.doDeleteCheckHealthyRs();
	}

}

